package com.wfsc.web.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.wfsc.web.entity.Encrypt;
import com.wfsc.web.util.SM4;

@RestController
@RequestMapping("/api")
public class ApiController {

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @ResponseBody
    @PostMapping("/test")
    public String postData(@RequestBody String requestData) {
        return "这是POST接口返回的数据";
    }

    @ResponseBody
    @PostMapping("/encrypt")
    public String getByJSON(@RequestBody JSONObject jsonParam) {

        String text = jsonParam.getString("text");
        String key = jsonParam.getString("key");

        // 直接将json信息打印出来
        // System.out.println(jsonParam.toJSONString());

        // 将获取的json数据封装一层，然后在给返回
        JSONObject result = new JSONObject();

        Encrypt encrypt = new Encrypt();

        SM4 sm4 = new SM4();

        encrypt.encryptData = sm4.encodeText(text, key);

        JSONObject data = new JSONObject();

        data.put("encryptData", encrypt.encryptData);

        result.put("msg", "success");
        result.put("code", 0);
        result.put("data", data);

        return result.toJSONString();
    }
}